<?php
declare (strict_types = 1);

namespace app\admincommon\controller;

use app\admincommon\service\AuthService;
use app\BaseController;

class AdminController extends BaseController{

    use \app\admincommon\traits\JumpTrait;

        /**
     * 当前模型
     * @Model
     * @var object
     */
    protected $model;
    

    /**
     * 初始化方法
     */
    protected function initialize(){
        $this->checkAuth();
    }

    /**
     * 检测权限
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function checkAuth(){

        $adminConfig = config('admin');
        $adminId = session('admin.id');
        $expireTime = session('admin.expire_time');
        
        $currentController = parse_name(app()->request->controller());
        $authService = app(AuthService::class);
        $currentNode = $authService->getCurrentNode();

        // 验证登录

        if (!in_array($currentController, $adminConfig['no_login_controller']) &&
            !in_array($currentNode, $adminConfig['no_login_node'])) {
                
            //dump("adminId".$adminId);

            empty($adminId) && $this->error('请先登录后台', [], __url('/index.php/admin/Login/index'));

            //判断是否登录过期
            if ($expireTime !== true && time() > $expireTime) {
                session('admin', null);
                $this->error('登录已过期，请重新登录', [] );
            }

        }

    }

}
